package com.hk.demo.player.http;



import com.zhoug.common.utils.MD5Utils;
import com.zhoug.common.utils.encrypt.AESHelper;
import com.zhoug.common.utils.encrypt.Base64Utils;
import com.zhoug.common.utils.encrypt.IEncryptor;
import com.zhoug.common.utils.encrypt.RSAHelper;

import java.nio.charset.Charset;
import java.util.UUID;

/**
 * 加密工具
 *
 * @Author 35574
 * @Date 2021/8/30
 * @Description
 */
public class MyEncryptorHelper {
    private static final String RSA_PUBLIC_KEY1 = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC3w+bvh70Dt2/m7cZnWaJ78A8TymxSCD4d/o7BhBL1Hs5lFzxJT3CHJNm65Qtj7rfsmye/dkd+wNFLZvqhLlGIuSWfZIXkjLy9eVwNI5tIiecnUdaKzKZgANW7xvPNoGfayW09CHrYte3JDLSfX4wrkroEx8767QbjZaioRIC5lQIDAQAB";
    private static final String RSA_PRIVATE_KEY1="";
    private static final String RSA_PUBLIC_KEY2="MIGJAoGBANK6DgUnWwtgNzvFRiKWnTW+CyZAv1fzCkkBquKbE0QG7MZBDzBI3L7v7sI61jrX5cvE9AWXLUlO0wn6Q+Qn92HVo1Y+Q5og0+MBVnoKihU4RUMZ5WDYNf7spFdS/uAS4OTKrr/CdiYX3nppHaB+GuGeU44ZnCOC7RoDqScHbwSXAgMBAAE=";
    private static final String RSA_PRIVATE_KEY2 = "MIICXQIBAAKBgQDSug4FJ1sLYDc7xUYilp01vgsmQL9X8wpJAarimxNEBuzGQQ8wSNy+7+7COtY61+XLxPQFly1JTtMJ+kPkJ/dh1aNWPkOaINPjAVZ6CooVOEVDGeVg2DX+7KRXUv7gEuDkyq6/wnYmF956aR2gfhrhnlOOGZwjgu0aA6knB28ElwIDAQABAoGBAJkuVnK4oR4r2vq/9LO/+JfQhhpQhsdQDPH1HFuNjvusbQHQ2VGoJY9GqrDWRFSSDKGLkjdeJbVcb1yZaDC6PMC3fUMT100JTZa08d2dYUfw5RTru5eHVI3lgAiy6ikdvhH+d17kREnaAjtyQADFiucYOFTm2mgy3VETd158KncBAkEA9ObDw0evB/ONNKlTldUmaqG0plWeLAOafWc9oI1eW9AwMe0tp1kSVu5/nAVC+eLv8OumYwoQ/4Q0Klq2t8SBBwJBANxGz7DhLKb77yDCi4b7qg1SXJ9oXWrq35ZTrehWGdqfITSrkxM6v+8g9TtB9mYbPtN0TSv6PW5bw9ccj5aXy/ECQQCSHUB9OT6fstrzqXb6NTL3ecUrXHfDcN7ypO8q5E2A3+RxVT3g047SmPc/4g/EwXZAuogKHQ5pNLpe7sKHP9FvAkAFGwMPV3jAehBl0vyG6WhcnK8W5sVbiAwcoN3kB7LbtVqArQVgqbjvIvVsJTA/15vSLNzpCwjHeQsF/lx5ohURAkAp4i4Qj8gZpzSPYy+Uvs8gzjNO6bQ6YwVeujlJsD1rWbFrIK99Vti31p7RmCve3WAL72pgKTUHrpiEfHH1zB6J";
    private static final String IV="6367585863675858";

    /**
     * 字符串编码方式
     */
    private static final Charset charset = BaseConstant.CHARSET;

    private static final int base64Flag = BaseConstant.BASE64_FLAG;

    private static final Base64Utils mBase64Utils;

    static {
        mBase64Utils = new Base64Utils(charset, base64Flag);
    }

    /**
     * base64编码工具
     *
     * @return
     */
    public static Base64Utils getBase64Utils() {
        return mBase64Utils;
    }

    /**
     * 生成AES加密的密匙 32个字节
     *
     * @return
     */
    public static String generatorAesKey() {
        String replace = UUID.randomUUID().toString().replace("-", "");
        return MD5Utils.getMD5(replace);
    }

    /**
     * 使用RAS加密AES的密匙
     *
     * @param key AES的密匙
     * @return 加密后的密匙
     */
    public static String encryptKey(String key) {
        IEncryptor iEncryptor = new RSAHelper.Builder()
                .setCharset(charset)
                .setBase64Flag(base64Flag)
                .setPublicKey(RSA_PUBLIC_KEY1)
                .build();
        return iEncryptor.encryptToBase64(key);
    }

    /**
     * 使用RAS解密AES的密匙
     *
     * @param key 被RAS加密后的AES密匙base64字符串
     * @return AES的密匙
     */
    public static String decryptKey(String key) {
        IEncryptor iEncryptor = new RSAHelper.Builder()
                .setCharset(charset)
                .setBase64Flag(base64Flag)
                .setPublicKey(RSA_PUBLIC_KEY2)
                .setPrivateKey(RSA_PRIVATE_KEY2)
                .build();
        return iEncryptor.decryptToString(key);
    }

    /**
     * 创建AES加密器
     * 用于数据加解密
     *
     * @param key AES密匙
     * @return
     */
    public static IEncryptor createAESHelper(String key) {
        return new AESHelper.Builder()
                .setCharset(charset)
                .setBase64Flag(base64Flag)
                .setTransformation("AES/CBC/PKCS7Padding")
                .setIv(IV)
                .setKey(key)
                .build();
    }


}
